summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/citra/citra_emu/camera/StillImageCameraHelper.java
blob: 701cb07104304de70cc6ea8e0c3f22c87dc3643d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

package org.citra.citra_emu.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;

import org.citra.citra_emu.NativeLibrary;
import org.citra.citra_emu.R;
import org.citra.citra_emu.activities.EmulationActivity;
import org.citra.citra_emu.utils.PicassoUtils;

import androidx.annotation.Nullable;

// Used in native code.
public final class StillImageCameraHelper {
    public static final int REQUEST_CAMERA_FILE_PICKER = 1;
    private static final Object filePickerLock = new Object();
    private static @Nullable
    String filePickerPath;

    // Opens file picker for camera.
    public static @Nullable
    String OpenFilePicker() {
        final EmulationActivity emulationActivity = NativeLibrary.sEmulationActivity.get();

        // At this point, we are assuming that we already have permissions as they are
        // needed to launch a game
        emulationActivity.runOnUiThread(() -> {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
            emulationActivity.startActivityForResult(
                    Intent.createChooser(intent,
                            emulationActivity.getString(R.string.camera_select_image)),
                    REQUEST_CAMERA_FILE_PICKER);
        });

        synchronized (filePickerLock) {
            try {
                filePickerLock.wait();
            } catch (InterruptedException ignored) {
            }
        }

        return filePickerPath;
    }

    // Called from EmulationActivity.
    public static void OnFilePickerResult(Intent result) {
        filePickerPath = result == null ? null : result.getDataString();

        synchronized (filePickerLock) {
            filePickerLock.notifyAll();
        }
    }

    // Blocking call. Load image from file and crop/resize it to fit in width x height.
    @Nullable
    public static Bitmap LoadImageFromFile(String uri, int width, int height) {
        return PicassoUtils.LoadBitmapFromFile(uri, width, height);
    }
}